package us.mooresr.webtroopmanager.client.mvp;

import us.mooresr.webtroopmanager.client.ClientFactory;
import us.mooresr.webtroopmanager.client.SecurityManager;
import us.mooresr.webtroopmanager.client.activity.AdminActivity;
import us.mooresr.webtroopmanager.client.activity.DashboardActivity;
import us.mooresr.webtroopmanager.client.activity.EditEventActivity;
import us.mooresr.webtroopmanager.client.activity.EditPersonActivity;
import us.mooresr.webtroopmanager.client.activity.EditRoleActivity;
import us.mooresr.webtroopmanager.client.activity.EditUserActivity;
import us.mooresr.webtroopmanager.client.activity.EditVehicleActivity;
import us.mooresr.webtroopmanager.client.activity.EventDriversActivity;
import us.mooresr.webtroopmanager.client.activity.EventListActivity;
import us.mooresr.webtroopmanager.client.activity.EventParticipantsActivity;
import us.mooresr.webtroopmanager.client.activity.ImportActivity;
import us.mooresr.webtroopmanager.client.activity.LoginActivity;
import us.mooresr.webtroopmanager.client.activity.PeopleListActivity;
import us.mooresr.webtroopmanager.client.activity.RegistrationActivity;
import us.mooresr.webtroopmanager.client.activity.RoleListActivity;
import us.mooresr.webtroopmanager.client.activity.UserListActivity;
import us.mooresr.webtroopmanager.client.activity.ValidationActivity;
import us.mooresr.webtroopmanager.client.activity.VehicleListActivity;
import us.mooresr.webtroopmanager.client.place.AdminPlace;
import us.mooresr.webtroopmanager.client.place.DashboardPlace;
import us.mooresr.webtroopmanager.client.place.EditEventPlace;
import us.mooresr.webtroopmanager.client.place.EditPersonPlace;
import us.mooresr.webtroopmanager.client.place.EditRolePlace;
import us.mooresr.webtroopmanager.client.place.EditUserPlace;
import us.mooresr.webtroopmanager.client.place.EditVehiclePlace;
import us.mooresr.webtroopmanager.client.place.EventDriversPlace;
import us.mooresr.webtroopmanager.client.place.EventListPlace;
import us.mooresr.webtroopmanager.client.place.EventParticipantsPlace;
import us.mooresr.webtroopmanager.client.place.ImportPlace;
import us.mooresr.webtroopmanager.client.place.LoginPlace;
import us.mooresr.webtroopmanager.client.place.PeopleListPlace;
import us.mooresr.webtroopmanager.client.place.RegistrationPlace;
import us.mooresr.webtroopmanager.client.place.RoleListPlace;
import us.mooresr.webtroopmanager.client.place.UserListPlace;
import us.mooresr.webtroopmanager.client.place.Validate;
import us.mooresr.webtroopmanager.client.place.VehicleListPlace;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.place.shared.Place;


public class AppActivityMapper implements ActivityMapper {

	private ClientFactory clientFactory;
	
	public AppActivityMapper(ClientFactory clientFactory) {
		super();
		this.clientFactory = clientFactory;
	}
	

	@Override
	public Activity getActivity(Place place) {

		/*
		 * Check for a place that doesn't require login
		 */
		
		if (place instanceof LoginPlace) 
			return new LoginActivity(clientFactory);
		/*
		else if (place instanceof ForgotPasswordPlace)
			return new ForgotPasswordActivity(clientFactory);
		*/
		else if (place instanceof RegistrationPlace)
			return new RegistrationActivity(clientFactory);
		else if (place instanceof Validate)
			return new ValidationActivity((Validate) place, clientFactory);
		
		
		
		// Check to make sure user is logged in.  If not, redirect to the login page.
		SecurityManager securityManager = clientFactory.getSecurityManager();
		if (securityManager.getUserId() == null) {
			return new LoginActivity(clientFactory);
		}
		
		
		if (place instanceof DashboardPlace) 
			return new DashboardActivity((DashboardPlace) place, clientFactory);
		else if (place instanceof PeopleListPlace)
			return new PeopleListActivity((PeopleListPlace) place, clientFactory);
		else if (place instanceof EventListPlace)
			return new EventListActivity((EventListPlace) place, clientFactory);
		else if (place instanceof VehicleListPlace) 
			return new VehicleListActivity((VehicleListPlace) place, clientFactory);
		else if (place instanceof EditPersonPlace)
			return new EditPersonActivity((EditPersonPlace) place, clientFactory);
		else if (place instanceof EditEventPlace) 
			return new EditEventActivity((EditEventPlace) place, clientFactory);
		else if (place instanceof AdminPlace) 
			return new AdminActivity(clientFactory);
		else if (place instanceof EditVehiclePlace) 
			return new EditVehicleActivity((EditVehiclePlace) place, clientFactory);
		else if (place instanceof EventDriversPlace)
			return new EventDriversActivity((EventDriversPlace) place, clientFactory);
		else if (place instanceof EventParticipantsPlace)
			return new EventParticipantsActivity((EventParticipantsPlace) place, clientFactory);
		else if (place instanceof EditRolePlace)
			return new EditRoleActivity((EditRolePlace) place, clientFactory);
		else if (place instanceof RoleListPlace) 
			return new RoleListActivity(clientFactory);
		else if (place instanceof UserListPlace)
			return new UserListActivity(clientFactory);
		else if (place instanceof EditUserPlace)
			return new EditUserActivity((EditUserPlace) place, clientFactory);
		else if (place instanceof ImportPlace)
			return new ImportActivity(clientFactory);
		
		return null;
	}
	
}
